<?php

class Model_State extends Model_Pandrade {

    const TABLE = "loc_state";

    /**
     * @var protected $id id value
     * @method int get_id() get_id(void)
     * @method void set_id() set_id(int $id)
     */
    protected $id;

    /**
     * @var protected $name name value
     * @method string get_name() get_name(void)
     * @method void set_name() set_name(string $name)
     */
    protected $name;

    /**
     * @var protected $acronym acronym value
     * @method string get_acronym() get_acronym(void)
     * @method void set_acronym() set_acronym(string $acronym)
     */
    protected $acronym;

    /**
     * @var protected $country country value
     * @method int get_country() get_country(void)
     * @method void set_country() set_country(int $country)
     */
    protected $country;

    /**
     * @return Model_Loc_state
     */
    public function __construct($id = 0) {
        if (0 < intval($id)) {
            $db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
            $db = $db->as_assoc()->execute();
            if ($db->count() > 0) {
                $this->load($db->current());
            }
        }
    }

    protected static function get_database_fields() {
        return array(
            'name' => __("Loc_state_Name"),
            'acronym' => __("Loc_state_Acronym"),
            'country' => __("Loc_state_Country"),
        );
    }

    public function validate() {
        $validateArray = array();
        foreach (self::get_database_fields() as $key => $value) {
            $validateArray[$key] = $this->get($key);
        }
        $validation = Validation::factory($validateArray)
        ;
        if ($validation->check()) {
            return $validateArray;
        } else {
            throw new Validation_Exception('validation', $validation);
        }
    }

    public static function get_report() {
        return DB::select(
                                "id"
                                , "name"
                                , "acronym"
                                , "country"
                                , array("id", "actions"))
                        ->from(self::TABLE)
                        ->order_by("id", "ASC");
    }

    public static function search_state($state = NULL) {
        if ($state === NULL) {
            return array();
        } else if ($state == "") {
            $states = DB::select(DB::expr("id, name, acronym"))->from(self::TABLE)
                            ->limit(15)->order_by("name", "ASC")->execute()->as_array();
            return $states;
        } else {
            //expressions with more than word be like: "%WORD1%WORD2%" (for mysql)
            $words = explode(" ", $state);
            $size = sizeof($words);
            $i = $size;
            $mysql_pattern = "";
            do {
//                $mysql_pattern .= is_array($words)?$words[$size - 1]:$words;
                $mysql_pattern .= $words[$size - $i];
                $mysql_pattern .= "%";
                $i--;
            } while ($i > 0);
            $states = DB::select(DB::expr("id, name, acronym"))->from(self::TABLE)
                            ->where("name", "LIKE", $mysql_pattern)
                            ->limit(15)->order_by("name", "ASC")->execute()->as_array();
            return $states;
        }
    }

}
